<template lang="html">
    <div class="goods">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i>&nbsp;藏品列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" round @click="urlAdd()"><i class="el-icon-plus"> </i>&nbsp;&nbsp;新增精品推荐</el-button>
            <div style="height:20px;"></div>
            <el-table :data="list" style="width: 100%" v-loading="loading">
                <el-table-column prop="id" label="ID" width="100"></el-table-column>
                <el-table-column label="封面图" width="120" align="center">
                    <template slot-scope="scope" >
                        <div><img :src="scope.row.head_img" style="width:100px;height:100px"></div>
                    </template>
                </el-table-column>
                <el-table-column prop="title_cn" label="标题"  align="center"></el-table-column>
                <el-table-column prop="price" label="价格"  align="center"></el-table-column>
                <el-table-column prop="stock" label="库存"  align="center"></el-table-column>
                <el-table-column prop="category_name1" label="分类"  align="center"></el-table-column>
                <el-table-column label="是否是精品推荐" width="180" align="center">
                     <template slot-scope="scope">
                        <div v-if="scope.row.is_recommend == 1"  @click="changeDelete(scope.row)" >是</div>
                        <div v-if="scope.row.is_recommend == 0"  @click="changeDelete(scope.row)" >否</div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope" >
                        <el-button v-show="scope.row.is_recommend == 1" size="small" type="text"  @click="CancelSetRe(scope.row)" >取消推荐</el-button>
                        <el-button class="check" size="small" type="text"  @click="ToDetail(scope.row)" >查看</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination" v-show="total>1">
                <el-pagination
                @current-change="handleCurrentChange"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import {getGoodList,getGoodDetail,postSetFine,postCancelSetFine,postSetRe,postCancelSetRe} from '@/api/goods/list'
    export default {
        name: "goods",
        data() {
            return {
                loading:true,//加载中
                type:1,//类型：0不限、1精品推荐、2每周精选
                class1:0,//一级分类ID、传0为全部
                pageIndex:1,
                pageSize:10,
                total:0,//总条数
                state:2,//只获取审核通过的,状态：0所有、1待审核、2审核通过、3审核不通过
                list:[],//藏品列表
            }
        },
        created(){
            //获取藏品列表
            this.getGoodList();
        },
        mounted() {
            
        },
        methods: {
            //获取藏品列表
            getGoodList(){
                getGoodList(this.state,this.type,this.class1,this.pageIndex,this.pageSize).then(res => {
                    // console.log(res);
                    this.list = [];
                    this.loading = false;//加载中
                    this.list = res.data.list;
                    this.total = res.data.total;
                })
            },
            //设置精品推荐
            changeSetRe(ev){
                postSetRe(ev.id).then(res => {
                    // console.log(res);
                    if(res.code == 200){
                        this.$message({
                            message: '设置成功！',
                            type: 'success'
                        });
                        this.getGoodList();//重新加载列表
                    }else {
                        this.$message.error('设置失败！');
                    }
                })
            },
            //取消精品推荐
            CancelSetRe(ev){
                postCancelSetRe(ev.id).then(res => {
                    if(res.code == 200){
                        this.$message({
                            message: '设置成功！',
                            type: 'success'
                        });
                        this.getGoodList();//重新加载列表
                    }else {
                        this.$message.error('设置失败！');
                    }
                })
            },
           //跳转详情
            urlAdd(ev){
                this.$router.push({path:'/goods',query:{type:this.type}});
            },
            //跳转详情
            ToDetail(ev){
                this.$router.push({path:'/goods/details',query:{id:ev.id}});
            },
            handleCurrentChange(index){
                this.loading = true;//加载中
                this.pageIndex = index;
                this.getGoodList();//重新加载
            },
        }
    }
</script>

<style lang="css" scoped>
.check {
    color: orange;
}
</style>
